﻿Imports System.Collections.Generic
Imports System.Data
Imports System.Data.SqlClient


''' <summary>
''' Clase de la capa de acceso a datos DAL que implementa el patrón Table Data Gateway para el manejo de la entidad Requisito
''' </summary>
''' <remarks></remarks>
Public Class TDGRequisito

    Private Shared m_Instance As TDGRequisito = Nothing


    Public Function Insert(conn As SqlConnection, Busq_Numero As Int16, Req_Descrip As String, Req_Detalle As String, _
                                                Optional ByRef Req_Item As Int16 = 0) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        Dim outParam_Req_Item As SqlParameter


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.addRequisito"
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@Req_Descrip", Req_Descrip)
        command.Parameters.AddWithValue("@Req_Detalle", Req_Detalle)

        outParam_Req_Item = command.Parameters.Add("@Req_Item", SqlDbType.SmallInt)
        outParam_Req_Item.Direction = ParameterDirection.InputOutput


        iRowsAffected = command.ExecuteNonQuery()


        ' Retorno valores
        Req_Item = outParam_Req_Item.Value

        Return iRowsAffected

    End Function

    Public Function Update(conn As SqlConnection, Busq_Numero As Int16, Req_Item As Int16, Req_Descrip As String, Req_Detalle As String) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.updRequisito"
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@Req_Item", Req_Item)
        command.Parameters.AddWithValue("@Req_Descrip", Req_Descrip)
        command.Parameters.AddWithValue("@Req_Detalle", Req_Detalle)


        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Delete(conn As SqlConnection, Busq_Numero As Int16, Req_Item As Int16) As Int16
        Dim command As New SqlCommand
        Dim iRowsAffected As Int16

        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "dbo.delRequisito"
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@Req_Item", Req_Item)

        iRowsAffected = command.ExecuteNonQuery()

        Return iRowsAffected

    End Function

    Public Function Find(conn As SqlConnection, Busq_Numero As Int16, Req_Item As Int16) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.getRequisito"
        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)
        command.Parameters.AddWithValue("@Req_Item", Req_Item)


        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Function FindAllPorBusqueda(conn As SqlConnection, Busq_Numero As Int16) As SqlDataReader
        Dim command As New SqlCommand
        Dim dr As SqlDataReader


        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure

        command.CommandText = "dbo.getRequisito_PorBusqueda_All"

        command.Parameters.AddWithValue("@Busq_Numero", Busq_Numero)

        dr = command.ExecuteReader()

        Return dr

    End Function

    Public Shared ReadOnly Property Instance()
        Get
            If (m_Instance Is Nothing) Then
                m_Instance = New TDGRequisito()
            End If

            Return m_Instance
        End Get
    End Property

End Class
